import 'dart:convert';

import 'package:fluttertoast/fluttertoast.dart';
import 'package:intl/intl.dart';

///显示Toast消息
void showToast(String message, {ToastGravity? gravity}) {
  if (message.isEmpty) return;
  Fluttertoast.showToast(msg: message, gravity: gravity ?? ToastGravity.CENTER);
}

extension StringExtension on String? {
  //
  // base64字符串=> 实际
  String base64StringtoString() {
    if (this?.contains(RegExp(r"[\u4e00-\u9fa5]")) ?? false) return this ?? '';
    final decodeBytes = base64.decode(this ?? '');
    final res = utf8.decode(decodeBytes);
    return res;
  }

  ///防止字段为空报错
  String em({String aft = ""}) {
    return (this ?? "").isEmpty ? aft : this!;
  }
}

extension DateString on DateTime? {
  String yyyyMMDDFormat({String gap = "-"}) {
    if (this == null) return "";
    String format = "yyyy${gap}MM${gap}dd HH:mm:ss";
    final dateF = DateFormat(format);
    return dateF.format(this!);
  }

  String ymdSMFormat({String gap = "-"}) {
    if (this == null) return "";
    String format = "yyyy${gap}MM${gap}dd HH:mm";
    final dateF = DateFormat(format);
    return dateF.format(this!);
  }
}

extension NumExtension on num? {
  ///防止字段为空报错
  num em() {
    return this == null ? 0 : this!;
  }

  ///数字转金额保留两位小数
  String toAmount() {
    return em().toStringAsFixed(2);
  }
}

extension IntExtension on int? {
  ///防止字段为空报错
  int em() {
    return this == null ? 0 : this!;
  }
}

extension DoubleExtension on double? {
  ///防止字段为空报错
  double em() {
    return this == null ? 0.0 : this!;
  }
}

extension ListExtension on List? {
  ///防止字段为空报错
  int em() {
    return this == null ? 0 : this!.length;
  }

  //是否是空
  bool empty() {
    return this == null || this?.isEmpty == true;
  }
}

extension MapExtension on Map? {
  ///防止字段为空报错
  int em() {
    return this == null ? 0 : this!.length;
  }
}
